package java数据访问对象模式;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        EmployeeDaoImpl employeeDao = new EmployeeDaoImpl();
        for (Employee employee : employeeDao.getAll()) {
            System.out.println("Emp: [No : " + employee.getId() + ", Name : "
                    + employee.getName() + " ]");
        }
        Employee employee = employeeDao.getAll().get(0);
        employee.setName("Jane");
        employeeDao.updateStudent(employee);

        employeeDao.get(0);
        System.out.println("Emp: [No : " + employee.getId() + ", Name : "
                + employee.getName() + " ]");
    }
}
class Employee{
    private String name;
    private int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
interface EmployeeDao{
    public List<Employee> getAll();
    public Employee get(int id);
    public void updateStudent(Employee student);
    public void delete(Employee student);
}
class EmployeeDaoImpl implements EmployeeDao{
    List<Employee> employeeList;

    public EmployeeDaoImpl() {
        employeeList = new ArrayList<>();
        Employee emp1 = new Employee("Jack", 0);
        Employee emp2 = new Employee("Tom", 1);
        employeeList.add(emp1);
        employeeList.add(emp2);

    }

    @Override
    public List<Employee> getAll() {
        return employeeList;
    }

    @Override
    public Employee get(int id) {
        return employeeList.get(id);
    }

    @Override
    public void updateStudent(Employee student) {
        employeeList.get(student.getId()).setName(student.getName());
        System.out.println("student:No" + student.getId() + ",update in the database");
    }

    @Override
    public void delete(Employee student) {
        employeeList.remove(student.getId());
        System.out.println("Employee: No" + student.getId() +",deleted from database");
    }
}
